package com.example.daytest.swing;

/**
 * @author heyu
 * @title: Jwing
 * @description:
 * @date 2023/3/30下午3:30
 */
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;


import javax.swing.*;

public class LinkLifecycleDetection {

    private JFrame frame;

    //定义每一行的数据
    private Vector lineData,colunNames;

    //定义每一行的数据
    private Vector rowdata;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    LinkLifecycleDetection window = new LinkLifecycleDetection();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public LinkLifecycleDetection() {
        initialize();
    }

    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 600, 400);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("链接生命周期检测");

        JPanel topPanel = new JPanel(new BorderLayout());
        frame.getContentPane().add(topPanel, BorderLayout.NORTH);

        JLabel title = new JLabel("电子渠道链接全生命周期卫士");
        title.setFont(new Font("Arial", Font.BOLD, 20));
        topPanel.add(title, BorderLayout.CENTER);

        JPanel buttonPanel = new JPanel(new FlowLayout());
        topPanel.add(buttonPanel, BorderLayout.EAST);

        JButton whiteListButton = new JButton("白名单检测");
        whiteListButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // 调用http接口并渲染数据
                renderDataFromApi("http://10.10.152.136:8081/services/api/link/white/query");
            }
        });
        buttonPanel.add(whiteListButton);

        JButton specialCheckButton = new JButton("专项检测");
        specialCheckButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // 调用http接口并渲染数据
                renderDataFromApi("http://10.10.152.136:8081/services/api/special/query?NAME=&LINK_URL=&LINK_TYPE=&STATE=");
            }
        });
        buttonPanel.add(specialCheckButton);

    }


    private void renderDataFromApi(String apiUrl) {
        // 先删除原来的面板
        Component[] components = frame.getContentPane().getComponents();
        for (Component component : components) {
            if (component instanceof JTabbedPane) {
                JTabbedPane tabbedPane = (JTabbedPane) component;
                tabbedPane.removeAll();
            }
        }
        JTabbedPane tabbedPane = new JTabbedPane();
        frame.getContentPane().add(tabbedPane, BorderLayout.CENTER);

        // 创建用于渲染数据的面板
        JPanel dataPanel = new JPanel(new BorderLayout());

        String string = null;
        try {
            string = HttpClientUtils.get(apiUrl);
        } catch (Exception e) {
            e.printStackTrace();
        }
        JSONObject all=JSONObject.parseObject(string);
        JSONObject data = all.getJSONObject("data");
        JSONArray arr=data.getJSONArray("pageData");

        colunNames=new Vector();
        colunNames.add("业务名称");
        colunNames.add("业务链接");
        colunNames.add("链接状态");
        colunNames.add("有效期开始时间");
        colunNames.add("有效期结束时间");

        rowdata=new Vector();

        for (Object object:arr){
            JSONObject ob=(JSONObject)object;
            lineData = new Vector();
            lineData.add(ob.getString("NAME")); //第一列数据
            lineData.add(ob.getString("LINK_URL")); //第二列数据
            lineData.add(ob.getString("STATE")); //第三列数据
            lineData.add(ob.getString("EFFECTIVE_START")); //第三列数据
            lineData.add(ob.getString("EFFECTIVE_END")); //第三列数据
            rowdata.add(lineData); //数据追加
        }

        JTable table=new JTable(rowdata,colunNames);
        dataPanel.add(table.getTableHeader(),BorderLayout.NORTH);
        dataPanel.add(table,BorderLayout.CENTER);
        tabbedPane.addTab("检测完成",dataPanel);
    }

}
